package com.csy.tcshop.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.csy.tcshop.product.entity.CategoryEntity;
import com.csy.tcshop.product.service.CategoryService;
import com.csy.common.utils.PageUtils;
import com.csy.common.utils.R;



/**
 * 商品三级分类
 *
 * @author 陈盛英
 * @email 1404630967@qq.com
 * @date 2020-12-10 00:25:44
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 三级分类列表查询，返回树状数据
     */
    @RequestMapping(value = "/list/tree",method = RequestMethod.GET)
    //@RequiresPermissions("product:category:list")
    public R listByTree(@RequestParam Map<String, Object> ams){
        List<CategoryEntity> res = categoryService.listByTree();
        return R.ok().put("data", res);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 根据拖拽功能进行层级修改排序修改等操作
     */
    @RequestMapping("/update/sortLevel")
    //@RequiresPermissions("product:category:update")
    public R updateSortLevel(@RequestBody CategoryEntity[] category){
        if(category==null||category.length==0){
            return R.error();
        }
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCascade(category);

        return R.ok();
    }

    /**
     * 删除
     *
     * @RequestBody ：获取请求体里面的内容，必须发送post请求
     * springmvc会主动将请求体里面的数据(JSON)，转化为我们对应的对象
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        //检查当前删除的分类是否在别的地方被引用
        categoryService.removeMenusByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
